package com.xht.transaction.service;

import com.xht.transaction.data.UserData;
import com.xht.transaction.dto.UserDTO;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存服务层
 */
@Service
public class StockService {

    @Autowired
    private UserData userData;

    
    @Transactional
    public Boolean stock(){
        Boolean result = userData.insert("扣减库存");
        int a = 10 / 0;
        return result;
    }


    @Transactional
    public Boolean stock2(){
        Boolean result = userData.insert("扣减库存");
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Boolean stockSupports(){
        Boolean result = userData.insert("扣减库存");
        int a = 10 / 0;
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean addLog(){
        Boolean result = userData.insert("新增库存");
        return result;
    }

    /**
     * 事务传播行为:NEVER
     * @return
     */
    @Transactional(propagation = Propagation.NEVER)
    public Boolean never(){
        Boolean result = userData.insert("扣减库存");
        int a = 10 / 0;
        return result;
    }

    /**
     * 事务传播行为:MANDATORY
     * @return
     */
    @Transactional(propagation = Propagation.MANDATORY)
    public Boolean mandatory(){
        Boolean result = userData.insert("扣减库存：mandatory");
        int a = 1 / 0;
        return result;
    }

    @Transactional(isolation = Isolation.SERIALIZABLE)
    public List<UserDTO> queryList(){
        List<UserDTO> list = userData.findByIdList(Lists.newArrayList(1, 2, 3, 4, 5, 6));
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Transactional(timeout = 3)
    public Boolean addStock(){
        Boolean result = userData.insert("新增库存");
        return result;
    }

}
